<?php
namespace app\app_manager\controller;

use app\app_manager\model\search\AppSearch;
use app\app_manager\model\validate\AppValidate;
use app\common\lib\AdminController;
use app\common\model\AppModel;

class AppController extends AdminController
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param();
            $searchM = new AppSearch();
            return $searchM->search($params);
        }
        return $this->fetch();
    }


    public function add() {
        if (request()->isPost()) {
            $data = $_REQUEST;
            if (empty($data['plat_key'])) {
                return json(['code' => 0, 'msg' => '应用KEY不能为空']);
            }
            // 生成应用ID
            $data['plat_id'] = AppModel::generatePlatId();
            // 生成密钥
            $data['plat_secret'] = AppModel::generatePlatSecret($data['plat_key']);
            $data['created_time'] = date('Y-m-d H:i:s', time());
            $data['updated_time'] = date('Y-m-d H:i:s', time());
            $validateM = new AppValidate();
            if (!$validateM->check($data)) {
                return json(['code' => 0, 'msg' => $validateM->getError()]);
            }
            $model = new AppModel($data);
            $res = $model->allowField(true)->save();
            if (!$res) {
                return json(['code' => 0, 'msg' => $model->getError()]);
            }
            return json(['code' => 1, 'msg' => 'success']);
        }

        return $this->fetch();
    }

    /**
     * 更新密钥
     *
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function update_secret()
    {
        $data = $_REQUEST;
        $model = new AppModel();

        if (request()->isPost()) {
            if (empty($data['plat_key'])) {
                return json(['code' => 0, 'msg' => '应用KEY不能为空']);
            }
            $data['plat_secret'] = AppModel::generatePlatSecret($data['plat_key']);
            $data['updated_time'] = date('Y-m-d H:i:s', time());
            $validateM = new AppValidate();
            if (!$validateM->scene('update_secret')->check($data)) {
                return json(['code' => 0, 'msg' => $validateM->getError()]);
            }
            $res = $model->allowField(true)->save($data, ['id' => $data['id']]);
            if (!$res) {
                return json(['code' => 0, 'msg' => $model->getError()]);
            }
            return json(['code' => 1, 'msg' => 'success']);
        }

        $Rs = $model->where(['id' => $data['id']])->find()->getData();
        $this->assign('Rs', $Rs);
        return $this->fetch();
    }

    public function update()
    {
        $data = $_REQUEST;
        $model = new AppModel();

        if (request()->isPost()) {
            $data['updated_time'] = date('Y-m-d H:i:s', time());
            $validateM = new AppValidate();
            if (!$validateM->check($data)) {
                return json(['code' => 0, 'msg' => $validateM->getError()]);
            }
            $res = $model->allowField(true)->save($data, ['id' => $data['id']]);
            if (!$res) {
                return json(['code' => 0, 'msg' => $model->getError()]);
            }
            return json(['code' => 1, 'msg' => 'success']);
        }

        $Rs = $model->where(['id' => $data['id']])->find()->getData();
        $this->assign('Rs', $Rs);
        $this->assign('status_list', AppModel::statusList());
        return $this->fetch();
    }

    public function lock() {
        $data = $_REQUEST;
        $model = new AppModel();
        $m = $model->where(['id' => $data['id']])->find()->getData();
        $status = $model::STATUS_OK;
        if ($m['status'] == $model::STATUS_OK) {
            $status = $model::STATUS_LOCK;
        }
        $model->where(['id' => $data['id']])->update(['status' => $status]);
        return json(['code' => 1, 'msg' => 'success']);
    }

}
